home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / misc / emu / p-interp.lha / p-interp-0.4 / turtlegr.c‾ < prev    next >
Text File  |  2001-06-06  |  7KB  |  311 lines

  1. /*
  2.  
  3.   P-Code interpreter (to run the apple pascal system)
  4.   Copyright (C) 2000 Mario Klebsch
  5.  
  6.   This program is free software; you can redistribute it and/or modify
  7.   it under the terms of the GNU General Public License as published by
  8.   the Free Software Foundation; either version 2 of the License, or
  9.   (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   GNU General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU General Public License
  17.   along with this program; if not, write to the Free Software
  18.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.  
  21.   $Log: turtlegr.c,v $
  22.   Revision 1.4  2001/06/06 23:14:19  mario
  23.   Turtlegraphics wird jetzt mit einem #define aktiviert
  24.  
  25.   Revision 1.3  2001/05/23 21:16:41  mario
  26.   Turtlegraphics wurde als eigener Prozess ausgelagert.
  27.  
  28.   Revision 1.2  2001/05/20 13:12:02  mario
  29.   CVS-Idents und Logs eingefügt
  30.  
  31.  
  32. */
  33.  
  34. #ifdef TURTLEGRAPHICS
  35.  
  36. #ident "$Id: turtlegr.c,v 1.4 2001/06/06 23:14:19 mario Exp $";
  37.  
  38. #include <math.h>
  39. #include <unistd.h>
  40. #include <stdio.h>
  41. #include <stdarg.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/wait.h>
  45.  
  46. #include "psystem.h"
  47. #include "pcode.h"
  48. #include "Memory.h"
  49. #include "Stack.h"
  50.  
  51. static FILE *TurtleServer=NULL;
  52.  
  53. #define TURTLE_SPEEDUP
  54.  
  55. #ifdef TURTLE_SPEEDUP
  56. static Integer    CurrentTurtleX;
  57. static Integer    CurrentTurtleY;
  58. static Integer    CurrentTurtleAng;
  59. #endif
  60.  
  61. void StartTurtleServer(void)
  62. {
  63.   int fd[2];
  64.   int pid;
  65.   int i;
  66.  
  67.   if (socketpair(PF_UNIX, SOCK_STREAM, 0, fd)<0)
  68.     {
  69.       perror("socketpair()");
  70.       return;
  71.     }
  72.   if ((pid=fork())<0)
  73.     {
  74.       perror("fork()");
  75.       return;
  76.     }
  77.   if (!pid)
  78.     {
  79.       if (fd[1]!=0)
  80.     {
  81.       close(0);
  82.       dup2(fd[1],0);
  83.     }
  84.       if (fd[1]!=1)
  85.     {
  86.       close(1);
  87.       dup2(fd[1],1);
  88.     }
  89.       for (i=getdtablesize();i>2;i--)
  90.     close(i);
  91.       execlp("xturtleserver", "xturtleserver", NULL);
  92.       execlp("./xturtleserver", "xturtleserver", NULL);
  93.       perror("xturtleserver");
  94.       _exit(1);
  95.     }
  96.   close(fd[1]);
  97.   TurtleServer=fdopen(fd[0], "wb+");
  98. }
  99.  
  100. void tprintf(char *format, ...)
  101. {
  102.   va_list ap;
  103.   if (!TurtleServer)
  104.     StartTurtleServer();
  105.  
  106.   if (TurtleServer)
  107.     {
  108.       va_start(ap, format);
  109.       vfprintf(TurtleServer, format, ap);
  110.       va_end(ap);
  111.       putc('\n', TurtleServer);
  112.     }
  113. }
  114.  
  115. Integer tgetint(void)
  116. {
  117.   int i=0;
  118.   char    Buffer[20];
  119.  
  120.   fflush(TurtleServer);
  121.  
  122.   for (i=0; i<19;i++)
  123.     {
  124.       Buffer[i]=getc(TurtleServer);
  125.       if (Buffer[i]=='\n')
  126.     break;
  127.     }
  128.  
  129.   Buffer[i]='\0';
  130.  
  131.   sscanf(Buffer, "%d", &i);
  132.   return ((Integer)i);
  133. }
  134.  
  135. #ifdef TURTLE_SPEEDUP
  136. void TurnTo(int Angle)
  137. {
  138.   CurrentTurtleAng=Angle;
  139.   while (CurrentTurtleAng<0)
  140.     CurrentTurtleAng+=360;
  141.   while (CurrentTurtleAng>=360)
  142.     CurrentTurtleAng-=360;
  143. }
  144. #endif
  145.  
  146. void TurtleGraphics(word EntryPoint)
  147. {
  148.   int    i;
  149.   char    func[64];
  150.   char    *p;
  151.  
  152.   i=0;
  153.   for (i=0; i<sizeof(func); i++)
  154.     if (!(func[i]=MemRdByte(EntryPoint,i)))
  155.       break;
  156.  
  157.   p=&func[16];
  158.   if (strcmp(p, "INITTURTLE")==0)
  159.     tprintf("INITTURTLE");
  160.   else if (strcmp(p, "TURN")==0)
  161.     {
  162.       Integer i=PopInteger();
  163.       tprintf("TURN %d",i);
  164. #ifdef TURTLE_SPEEDUP
  165.       TurnTo(CurrentTurtleAng+i);
  166. #endif
  167.     }
  168.   else if (strcmp(p, "TURNTO")==0)
  169.     {
  170.       Integer i=PopInteger();
  171.       tprintf("TURNTO %d",i);
  172. #ifdef TURTLE_SPEEDUP
  173.       TurnTo(i);
  174. #endif
  175.     }
  176.   else if (strcmp(p, "MOVE")==0)
  177.     {
  178.       Integer i=PopInteger();
  179.       tprintf("MOVE %d",i);
  180. #ifdef TURTLE_SPEEDUP
  181.       CurrentTurtleX+=rint(cos(CurrentTurtleAng*3.14/180)*i);
  182.       CurrentTurtleY+=rint(sin(CurrentTurtleAng*3.14/180)*i);
  183. #endif
  184.     }
  185.   else if (strcmp(p, "MOVETO")==0)
  186.     {
  187.       Integer y=PopInteger();
  188.       Integer x=PopInteger();
  189.       tprintf("MOVETO %d %d",x,y);
  190. #ifdef TURTLE_SPEEDUP
  191.       CurrentTurtleX=x;
  192.       CurrentTurtleY=y;
  193. #endif
  194.     }
  195.   else if (strcmp(p, "PENCOLOR")==0)
  196.     tprintf("PENCOLOR %d",Pop());
  197.   else if (strcmp(p, "TEXTMODE")==0)
  198.     tprintf("TEXTMODE");
  199.   else if (strcmp(p, "GRAFMODE")==0)
  200.     tprintf("GRAFMODE");
  201.   else if (strcmp(p, "FILLSCREEN")==0)
  202.     tprintf("FILLSCREEN %d",Pop());
  203.   else if (strcmp(p, "VIEWPORT")==0)
  204.     {
  205.       int yMax=PopInteger();
  206.       int yMin=PopInteger();
  207.       int xMax=PopInteger();
  208.       int xMin=PopInteger();
  209.       tprintf("VIEWPORT %d %d %d %d",xMin, xMax, yMin, yMax);
  210.     }
  211.   else if (strcmp(p, "TURTLEX")==0)
  212.     {
  213.       Pop();
  214.       Pop();
  215. #ifdef TURTLE_SPEEDUP
  216.       Push(CurrentTurtleX);
  217. #else
  218.       tprintf("TURTLEX");
  219.       Push(tgetint());
  220. #endif
  221.     }
  222.   else if (strcmp(p, "TURTLEY")==0)
  223.     {
  224.       Pop();
  225.       Pop();
  226. #ifdef TURTLE_SPEEDUP
  227.       Push(CurrentTurtleY);
  228. #else
  229.       tprintf("TURTLEY");
  230.       Push(tgetint());
  231. #endif
  232.     }
  233.   else if (strcmp(p, "TURTLEANG")==0)
  234.     {
  235.       Pop();
  236.       Pop();
  237. #ifdef TURTLE_SPEEDUP
  238.       Push(CurrentTurtleAng);
  239. #else
  240.       tprintf("TURTLEANG");
  241.       Push(tgetint());
  242. #endif
  243.     }
  244.   else if (strcmp(p, "SCREENBIT")==0)
  245.     {
  246.       Integer y=PopInteger();
  247.       Integer x=PopInteger();
  248.       tprintf("SCREENBIT %d %d",x ,y);
  249.       Push(tgetint());
  250.     }
  251.   else if (strcmp(p, "DRAWBLOCK")==0)
  252.     {
  253.       int    x;
  254.       int    y;
  255.       Integer Mode    = PopInteger();
  256.       Integer YScreen = PopInteger();
  257.       Integer XScreen = PopInteger();
  258.       Integer Height  = PopInteger();
  259.       Integer Width   = PopInteger();
  260.       Integer YSkip   = PopInteger();
  261.       Integer XSkip   = PopInteger();
  262.       Integer RowSize = PopInteger();
  263.       word Source     = Pop();
  264.  
  265.       tprintf("DRAWBLOCK %d %d %d %d %d %d %d %d",
  266.           (((XSkip+Width+7)&~7)-(XSkip&~7))>>3, XSkip&7, 0,
  267.           Width, Height, XScreen, YScreen, Mode);
  268.       for (y=0; y<Height; y++)
  269.     for (x=(XSkip&7);x<((XSkip+Width+7)&~7);x+=8)
  270.       putc(MemRdByte(Source, (y+YSkip)*RowSize+x/8), TurtleServer);
  271.     }
  272.   else if (strcmp(p, "WCHAR")==0)
  273.     {
  274.       tprintf("WCHAR %d",Pop()&0xff);
  275. #ifdef TURTLE_SPEEDUP
  276.       CurrentTurtleX += 7;
  277. #endif
  278.     }
  279.   else if (strcmp(p, "WSTRING")==0)
  280.     {
  281.       word Addr=Pop();
  282.       int  Len=MemRdByte(Addr, 0);
  283.       int  i;
  284.  
  285.       tprintf("WSTRING %d",Len);
  286.       for (i=0;i<Len; i++)
  287.     putc(MemRdByte(Addr, i+1), TurtleServer);
  288. #ifdef TURTLE_SPEEDUP
  289.       CurrentTurtleX += 7*Len;
  290. #endif
  291.     }
  292.   else if (strcmp(p, "CHARTYPE")==0)
  293.     tprintf("WCHAR %d",Pop()&0x0f);
  294.   else
  295.     XeqError(XNOTIMP);
  296.   fflush(TurtleServer);
  297. }
  298.  
  299. void GraphicsClear(void)
  300. {
  301.   int status;
  302.   if (TurtleServer)
  303.     {
  304.       fclose(TurtleServer);
  305.       TurtleServer=NULL;
  306.       wait(&status);
  307.     }
  308. }
  309.  
  310. #endif /* TURTLEGRAPHICS */
  311.